package com.xiaomi.service.Impl;

import com.xiaomi.dao.UserDao;
import com.xiaomi.pojo.User;
import com.xiaomi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Author xiao宋
 * @Date 19:25  2022/9/13
 * @Version 1.0
 * @Description TODO
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        System.out.println(user);
        return user;
    }

    @Override
    public boolean register(User user) {
//        等于空则添加失败
        return userDao.addUser(user) > 0;
    }

    @Override
    public User findByUserAndPwd(String username, String password) {
        return userDao.findUserAndPwd(username, password);
    }

    public User findAdminUser(String username, String password) {
        return userDao.findUserAndPwd(username, password);
    }

    // flag 1 会员 0管理员 2无效会员
    @Override
    public List<User> gerUserList() {
        List<User> list = userDao.getUserList();
        List<User> listvip = new ArrayList<>();
        for (User user : list) {
            if (user.getFlag() != 2) {
                listvip.add(user);
            }
        }
        return listvip;
    }

    @Override
    public List<User> gerInvaldUser() {
        List<User> list = userDao.getUserList();
        List<User> listinvald = new ArrayList<>();
        for (User user : list) {
            if (user.getFlag() == 2) {
                listinvald.add(user);
            }
        }
        return listinvald;
    }

    @Override
    public void deleteUserById(String id) {
        userDao.deleteUserById(id);
    }

    /**
     * 查找用户，通过名字性别，
     * </p> 0 vip  1 失效用户
     *
     * @param username
     * @param gender
     * @param options
     * @return
     */
    @Override
    public List<User> getUserByName(String username, String gender, int options) {
        List<User>userList;
        if (options == 0) {
            userList = gerUserList();
        }else {
            userList = gerInvaldUser();
        }

        List<User> list = new ArrayList<>();

        for (User user : userList) {
            if (username != "" ||username.length()==0 || gender != ""||gender.length()==0 ) {
                if (username.equalsIgnoreCase(user.getUsername()) && gender.equalsIgnoreCase(user.getGender())) {
                    list.add(user);
                } else if (username == null||username.length()==0 && gender.equalsIgnoreCase(user.getGender())) {
                    list.add(user);
                } else if (gender == null && username.equalsIgnoreCase(user.getUsername())) {
                    list.add(user);
                }
            }else {
                list=userList;
            }
        }
        return list;
    }

}
